package Bench::Test::View::Latex;

use 5.008004;
use strict;
use warnings;

use Template;

sub process {
   my $data = shift;
   my $output;

   my $config = {
      INTERPOLATE => 1,  # expand "$var" in plain text
      POST_CHOMP  => 0,  # cleanup whitespace
      PRE_CHOMP   => 0,
      EVAL_PERL   => 1,  # evaluate Perl code blocks
   };

   my $template = Template->new($config);

   my $vars = {
      data => $data,
   };

   # process input template, substituting variables
   $template->process(\*DATA, $vars, \$output)
     || die $template->error();

   return $output;
}


1;

# Template for LaTeX output

__DATA__
[%- PERL -%]
   use Data::Dumper;
   my $data   = $stash->get('data');
   my $header = shift @{$data};
   my $align  = "c" x (@$header);
   for (@$header) {
      s/_/\\_/;
   }
   $stash->set('header',  $header);
   $stash->set('rowtype', 'rowB');
   $stash->set('align',   $align);
[%- END -%]
\begin{table}
\begin{center}
\begin{tabular}{|[% align %]|}
\hline
[% header.0 -%]
   [%- FOREACH index IN [ 1..header.max ] -%]
 & [% header.$index -%]
   [%- END %]\\
\hline
\hline
[% FOREACH row IN data -%]
   [%- row.0 -%]
   [%- FOREACH index IN [ 1..row.max ] -%]
 & [% row.$index -%]
   [%- END %]\\
\hline
[% END -%]
\end{tabular}
\end{center}
\caption[Bench::Test table]{Table generated by Bench::Test}
\end{table}
